package form.ui {
	import flash.events.Event;
	import flash.text.TextFormatAlign;
	
	import form.dispatchers.FormElementDispatcher;
	import form.dispatchers.RadioDispatcher;
	import form.events.FormElementChangeEvent;
	import form.events.FormElementEvent;
	import form.events.RadioEvent;
	import form.interfaces.IFormElement;
	import form.ui.component.Radio.RadioOff;
	import form.ui.component.Radio.RadioOn;
	import form.ui.core.ButtonToggle;

	public class Radio extends ButtonToggle implements IFormElement {
		private var _formName:String;
		private var _formValue:String;
		
		private var _checkedOnAddedToStage:Boolean = false;
		private var _disabledOnAddedToStage:Boolean = false;
		
		public function Radio() {
			super();
						
			this.on = new RadioOn();
			this.off = new RadioOff();
			
			this.enable();
			
			this.labelLeft = this.on.width + 2;
			this.labelTop = -1;
			this.labelAlign = TextFormatAlign.LEFT;
			
			RadioDispatcher.instance.addEventListener(RadioEvent.CHECKED, this.onRadioChecked);
			
			var e:FormElementEvent = new FormElementEvent(FormElementEvent.INITIALIZED);
			e.element = this;
			
			FormElementDispatcher.instance.dispatchEvent(e);
		}
		
		public function set formName(value:String):void {
			this._formName = value;
		}
		
		public function get formName():String {
			return this._formName;
		}
		
		public function set formValue(value:String):void {
			this._formValue = value;	
		}
		
		public function get formValue():String {
			if (this.isOn) {
				return this._formValue;
			} else {
				return null;
			}
		}
		
		public function get selected():Boolean { return this.isOn; }
		
		public function set checkedOnAddedToStage(value:Boolean):void {
			this._checkedOnAddedToStage = value;
		}
		
		public function get checkedOnAddedToStage():Boolean {
			return this._checkedOnAddedToStage;	
		}
		
		public function set disabledOnAddedToStage(value:Boolean):void {
			this._disabledOnAddedToStage = value;
		}
		
		public function get disabledOnAddedToStage():Boolean {
			return this._disabledOnAddedToStage;	
		}
		
		public override function onAction():void {
			var e1:RadioEvent = new RadioEvent(RadioEvent.CHECKED);
			e1.radio = this; 
			
			RadioDispatcher.instance.dispatchEvent(e1);
			
			var e2:FormElementChangeEvent = new FormElementChangeEvent(FormElementChangeEvent.RADIO_ON);
			e2.element = this; 
			
			this.dispatchEvent(e2);
		}
		
		public override function offAction():void {
			var e:FormElementChangeEvent = new FormElementChangeEvent(FormElementChangeEvent.RADIO_OFF);
			e.element = this; 
			
			this.dispatchEvent(e);
		}
		
		protected override function onAddedToStage(e:Event):void {
			if (!this.checkedOnAddedToStage) {
				this.turnOff();
			} else {
				this.turnOn();
			}
			
			if (!this.disabledOnAddedToStage) {
				this.enable();
			} else {
				this.disable();
			}
		}
		
		protected override function onMouseUp(e:Event):void {
			if (this.isOn) { return; }
			
			super.onMouseUp(e);
		}
		
		private function onRadioChecked(e:RadioEvent):void {
			if (e.radio == null || e.radio.formName == null || e.radio.formName != this.formName) {
				return;
			}
			
			if (e.radio != this && this.isOn) {
				this.turnOff();
			}
		}
		
		
	}
}